********************************************************************************

// Indicate your working directory

clear all
global beg_path  `"XXX"' 
set more off

********* Create a folder for datasets prepared for the merge

mkdir `"${beg_path}Data\Micro\CES\To merge"' 

******** 1965

use `"${beg_path}Data\Micro\CES\CES-E-1965\CES-E-1965_F1.dta"', clear

fre V335
recode V262 (0=0 "abstention") (11/19 = 1 "voted") (else =.), gen(participation)

gen yob = 1965-V335
gen election_year = 1965
gen id_var = V2 

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1965_to_merge.dta"', replace

******** 1968

use `"${beg_path}Data\Micro\CES\CES-E-1968\CES-E-1968_F1.dta"', clear

fre var179
recode var179 (2=0 "abstention") (1 = 1 "voted") (else =.), gen(participation)

gen yob = 1968-age
gen election_year = 1968
gen id_var = var003

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1968_to_merge.dta"', replace


******** 1974-1980
use `"${beg_path}Data\Micro\CES\CES-E-1974-1980\CES-E-1974-1980_F1.dta"', clear
gen yob = 1979-v1535 // This is age in 1979 (see p. 1695 in the codebook)
gen id_var = CASEID

preserve 
gen election_year = 1974 if inlist(v1013, 1)
recode v160 (2=0 "abstention") (1 = 1 "voted") (else =.), gen(participation)
keep if inlist(v1013, 1) 
keep id_var election_year yob participation
keep if yob !=. & participation !=. 
save `"${beg_path}Data\Micro\CES\To merge\1974_to_merge.dta"', replace
restore 

preserve 
gen election_year = 1979 if inlist(v1013, 2, 3)  
recode v1233 (2=0 "abstention") (1 = 1 "voted") (else =.), gen(participation)
keep if inlist(v1013, 2, 3) 
keep id_var election_year yob participation
keep if yob !=. & participation !=. 
save `"${beg_path}Data\Micro\CES\To merge\1979_to_merge.dta"', replace
restore 

preserve 
gen election_year = 1980 if inlist(v1013, 2, 3)  
recode v2061 (2=0 "abstention") (1 = 1 "voted") (else =.), gen(participation)
keep if inlist(v1013, 2, 3) 
keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1980_to_merge.dta"', replace
restore 

******** 1984

use `"${beg_path}Data\Micro\CES\CES-E-1984\CES-E-1984_F1.dta"', clear

recode var124 (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace var437 = var438 if var437==0

gen yob = 1984-var437
gen election_year = 1984
gen id_var = var001 

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1984_to_merge.dta"', replace

******** 1988

use `"${beg_path}Data\Micro\CES\CES-E-1988\CES-E-1988_F1.dta"', clear

recode xb1 (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

gen yob = n1
gen election_year = 1988
gen id_var = idnum

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1988_to_merge.dta"', replace

******** 1993

use `"${beg_path}Data\Micro\CES\CES-E-1993\CES-E-1993_F1.dta"', clear
recode PESA2 (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

gen yob = PESAGE
gen election_year = 1993
gen id_var = IDNUM


keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1993_to_merge.dta"', replace

*** 1997

use `"${beg_path}Data\Micro\CES\CES-E-1997\CES-E-1997_F1.dta"', clear

recode pesa2a (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)
replace participation = 1 if pesa2b == 1 & pesa2a==.
replace participation = 0 if pesa2b == 5 & pesa2a==.

gen yob = pesage
gen election_year = 1997
gen id_var = idnum

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\1987_to_merge.dta"', replace

******** 2000

use `"${beg_path}Data\Micro\CES\CES-E-2000\CES-E-2000_F1.dta"', clear

recode pesa2 (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

gen yob = cpsage
gen election_year = 2000
gen id_var = idnum

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\2000_to_merge.dta"', replace

***** 2004

use `"${beg_path}Data\Micro\CES\CES-E-2004\CES-E-2004_F1.dta"', clear

recode pes_a2a (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

gen yob = cps_s1
gen election_year = 2004
gen id_var = idnum

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\2004_to_merge.dta"', replace

*** 2008

use `"${beg_path}Data\Micro\CES\CES-E-2008\CES-E-2008_F1.dta"', clear
recode ces08_PES_B1 (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace YEARofBIRTH  = . if YEARofBIRTH >1990

gen yob = YEARofBIRTH
gen election_year = 2008
gen id_var = ces08_IDNUM

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\2008_to_merge.dta"', replace

*** 2011

use `"${beg_path}Data\Micro\CES\CES-E-2011\CES-E-2011_F1.dta"', clear

recode PES11_3 (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

gen yob = CPS11_78
gen election_year = 2011
gen id_var = CES11_IDNUM

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\2011_to_merge.dta"', replace

*** 2015

use `"${beg_path}Data\Micro\CES\CES-E-2015\CES-E-2015_F1.dta"', clear

recode p_voted (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)
replace participation = 1 if p_votedlong ==1 & participation ==.  
replace participation = 0 if p_votedlong ==2 & participation ==. 

replace age = . if age ==1000

gen yob = age
gen election_year = 2015
gen id_var = respID
destring id_var, force replace

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\CES\To merge\2015_to_merge.dta"', replace


